import sys

def	checkWindow(attribute,window):
	#print attribute
	strAttr = attribute.split('_')
	for i in strAttr:
		try:
			value = int(i)
			#print value
			if (value >= window) & (value <= -window):
				#print ' True'
				return True
			else:
				#print ' False'
				return False
		except:
			if i == strAttr[-1]:
				#print ' True'
				return True

def checkFeature(attribute, feature):
	index = attribute.find(feature)
	isFound = False
	if index != -1:
		if ((index == 0) | (attribute[index-1]=='_')):

			if index ==0:
				isFound = attribute[index+len(feature):].isdigit()
				if not isFound:
					isFound = len(attribute) == len(feature)
				
			if((attribute[index-1]=='_')):
				try:
					int(attribute[0:index-1])
					isFound = True
				except:
					try:
						int(attribute[0:index-3])
						isFound = True
					except:
						if((len(attribute[0:index-1])==1)&(attribute[0:index-1].isalpha())):
							isFound = True
						else:
							isFound = False
			if isFound:
				if (index+len(feature)) == len(attribute):
					isFound = True;			
				else:
					try:
						#print attribute
						int(attribute[index+len(feature):])
						isFound = True
					except:
						isFound = False
		else:
			isFound = False
	else:
		isFound = False
	return isFound


def getIndices(filename,window,features_str):
	attributeList = []
	attributeLines=[]
	sampleList = []
	proteinList = []
	featureColumns = ''
	proteinRange = []

	features = []
	window = -(int(window)/2)
	givenFt = features_str.split(',')
	for i in range(0,len(givenFt)):
		features.append(givenFt[i])

	
	filereader = open(filename, 'rb')
	row = filereader.readline()
	index = 0
	# Read file until to encounter @DATA line, store attributes
	while not row.find("@DATA") != -1:
		if	row.find("@ATTRIBUTE") != -1:
			attributeList.append(row.split()[1])
			#attributeLines.append(row)
			#print attributeList[-1]
		row = filereader.readline()
		index = index +1
	# Read file until to EOF , store proteinNames
	proInd = 0
	for	row in filereader:
		splittedRow = row.split(",")
		sampleList.append(row)
		name = row.split("_")[0]+'_'+row.split("_")[1]
		if not proteinList:
			proteinList.append(name)
			proteinRange.append(name + ',0,') # first Protein
		if	name != proteinList[-1]:
			proteinRange[-1] = proteinRange[-1] + str(proInd-1)
			proteinRange.append(name + ','+str(proInd)+',')
			proteinList.append(name)
		proInd = proInd +1

	proteinRange[-1] = proteinRange[-1] + str(proInd-1)
	#print proteinRange
	# Calculate the number of proteins and find the number of each bins
	numPro = len(proteinList)

	#Create index array
	
	indexArr = range(0,numPro)


	#Write Attributes

	for i in range(0,len(features)):
		for j in range(0,len(attributeList)):
			if	checkFeature(attributeList[j],features[i]):
				if checkWindow(attributeList[j],window):
					if not featureColumns:
						 featureColumns = str(j+1)
					else:
						featureColumns = featureColumns + ','+str(j+1)
					#print attributeList[j]
	return featureColumns,proteinRange

if __name__ == '__main__':
	filename = sys.argv[1]
	window   = sys.argv[2]
	ft	     = sys.argv[3]
	ftCol,proRange = getIndices(filename,window,ft)
	#print ftCol
